package com.markyao.mq;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {

	public static final String MIAOSHA_QUEUE = "seckill.queue";


	@Bean("jacksonConverter")   //直接创建一个用于JSON转换的Bean
    public Jackson2JsonMessageConverter converter(){
        return new Jackson2JsonMessageConverter();
   }
   @Bean("miaoshaExchange")     //定义交换机 可以多个
    public Exchange exchange(){
        return ExchangeBuilder.directExchange("amq.direct").build();
    }
	@Bean(MIAOSHA_QUEUE)
	public Queue queue() {
		//创建秒杀队列
		return QueueBuilder.
				durable(MIAOSHA_QUEUE).
				build();

	}
	@Bean("binding")
    public Binding binding(@Qualifier("miaoshaExchange") Exchange exchange,
						   @Qualifier(MIAOSHA_QUEUE) Queue queue){
        //将定义好的交换机和队列进行绑定
        return BindingBuilder
                .bind(queue)
                .to(exchange)
                .with("miaosha")    //定义routingKey
                .noargs();
    }
}